package com.yonyou.pmclouds.basecom.runtime;

import com.alibaba.dubbo.rpc.RpcContext;

import java.util.HashMap;
import java.util.Map;

public class RpcUtils {
    private RpcUtils() {

    }

    /**
     * 根据当前线程的context，设置到dubbo的调用上下文中
     */
    public static void setRpcContextFromRuntimeEnv() {
        Map<String, String> context = new HashMap<>();
        context.put(RuntimeEnvironment.KEY_DATA_SOURCE, RuntimeEnvironment.getDataSource());
        if (RuntimeEnvironment.getLanguage() != null) {
            context.put(RuntimeEnvironment.KEY_LANG, RuntimeEnvironment.getLanguage().getValue().getLanguage());
            context.put(RuntimeEnvironment.KEY_COUNTRY, RuntimeEnvironment.getLanguage().getValue().getCountry());
        }
        context.put(RuntimeEnvironment.KEY_LOGIN_USER, RuntimeEnvironment.getLoginUserId());
        context.put(RuntimeEnvironment.KEY_TENANT_ID, RuntimeEnvironment.getTenantId());
        context.put(RuntimeEnvironment.KEY_SYS_ID, RuntimeEnvironment.getSysId());
        RpcContext.getContext().setAttachments(context);
    }
}
